package myJlite.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jvlite.core.JLiteContext;
import jvlite.filter.FilterRequestContext;
import jvlite.mvc.annotaion.Action;
import jvlite.mvc.annotaion.ActionMetod;
import jvlite.mvc.annotaion.ClzAuth;
import jvlite.mvc.annotaion.Insert;
import jvlite.mvc.annotaion.MethodAuth;
import jvlite.mvc.annotaion.PointCut;
import jvlite.render.IRender;
import jvlite.util.BeanUtil;
import jvlite.util.LogUtil;
import myJlite.entity.ExamVO;
import myJlite.render.ExamRender;
import myJlite.service.ExamService;
import myJlite.service.IExamService;

@Action(value = "exam")
@PointCut(advice = "examAdvice", value = "")
@ClzAuth(requirePower={"emplorer"})
public class ExamCtrl implements IExamCtrl {

	@Insert(value = "examService")
	private IExamService examService;
	 @Insert(value = "examRender")
	public IRender render;

	@Override
	@ActionMetod("login")
	// @MethodAuth(requirePower="user")
	public String login(HttpServletRequest request, HttpServletResponse response) {
		String target = null;
		ExamVO examVO = null;
		try {
			examVO = (ExamVO) BeanUtil.reqToBean(request, ExamVO.class, true);
			ExamVO vo = examService.getByVO(examVO);
			// config hasAuthority
			examService.configDataSource(examVO);
			render.renderData(request, examVO);
			if (vo == null) {
				target = "/login.jsp";
			} else {
				target = "/index.jsp";
			}
			// enable authority
			JLiteContext.isAuthority = true;
		} catch (Throwable e) {
			this.noAuthForward(request, response);
			e.printStackTrace();
		}
		return target;
	}

	@ActionMetod("outJson")
	@MethodAuth(requirePower="manager")
	public void renderJson(HttpServletRequest request,
			HttpServletResponse response) {
		// LogKit.info("id:"+context.getRequest().getAttribute("id").toString());
		try {
			ExamVO examVO = (ExamVO) BeanUtil.reqToBean(request, ExamVO.class,
					true);
			ExamVO vo = examService.getByVO(examVO);
			this.render.renderJSON(response, vo);
		} catch (Throwable e) {
			this.noAuthForward(request, response);
		}
	}

	@ActionMetod("addUser")
	// @MethodAuth(requirePower={"admin","user"})
	public String create(HttpServletRequest request,
			HttpServletResponse response) {
		String targetURL = null;
		try {
			ExamVO examVO = (ExamVO) BeanUtil.reqToBean(request, ExamVO.class,
					true);
			// Object object = ProxyExecutor.exec("main.service.ExamService",
			// examService.getClass().getMethod("create", ExamVO.class), new
			// Object[]{examVO});
			Integer usrCnt = examService.create(examVO);
			targetURL = "/create.jsp";
			// Integer usrCnt = (Integer) object;
			this.render.renderData(request, usrCnt);
		} catch (Throwable e) {
			this.noAuthForward(request, response);
		}
		return targetURL;
	}

	@ActionMetod("logout")
	public void logout(HttpServletRequest request, HttpServletResponse response) {
		Map<String, String[]> parameterMap = FilterRequestContext.get().getRequest().getParameterMap();
		for (Object key : parameterMap.keySet()) {
			LogUtil.info(key+":"+parameterMap.get(key)+"\n");
		}
		try {
			ExamVO vo = (ExamVO) BeanUtil.reqToBean(request, ExamVO.class,
					true);
			ExamVO examVO = new ExamService().getByID(vo.getId());
			this.render = new ExamRender();
			render.renderJSON(response, examVO.getUsername()+" logout successfully!");
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
//		this.render.renderJSON(response, new String("logout successfully!"));
	}

	private void noAuthForward(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			request.getRequestDispatcher("/error.jsp").forward(request,
					response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@ActionMetod("mybatis")
	public void mybatis(HttpServletRequest request,
			HttpServletResponse response) throws SQLException{
		HttpServletRequest req = FilterRequestContext.get().getRequest();
		List<ExamVO> list = examService.mybatis(req.getParameter("username"));
		LogUtil.info(Arrays.toString(list.toArray()));
		this.render.renderJSON(response, list);
	}
	
}